package com.processmining.util;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;


public class JarUtil  {  
	
	private URLClassLoader loader;
	
    public JarUtil(URL[] url) {  
       loader = new URLClassLoader(url);
    }  
    public JarUtil(URL[] url,ClassLoader loader){
    	loader = new URLClassLoader(url, loader);
    }

    /**
     * 得到构造函数
     * @param className	类全限定名
     * @param args		构造函数参数
     * @return
     * @throws ClassNotFoundException
     * @throws NoSuchMethodException
     * @throws SecurityException
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     * @throws InvocationTargetException
     */
    public Constructor getConstructor(String className,Class... args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		
    	Class clazz = loader.loadClass(className);							// 载入指定类，注意一定要带上类的包名
		Constructor c = clazz.getConstructor(args); 						// 构造函数
		
		return c;
    }
    
    
    
    public Method getMethod(Class cls,String methodName,Class... args) throws NoSuchMethodException, SecurityException{
    	
    	Method method = cls.getMethod(methodName, args);
 
    	return method;
    }
    
    /**
     * 调用无参方法
     * 直接返回结果
     * @throws SecurityException 
     * @throws NoSuchMethodException 
     * @throws InvocationTargetException 
     * @throws IllegalArgumentException 
     * @throws IllegalAccessException 
     */
    public Object invokeMethodWithoutParam(Object obj,String methodName) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
    	
    	Method method = obj.getClass().getMethod(methodName);
    	Object ret = method.invoke(obj);
    	
    	return ret;
    }
    
    /**
     * 获取类中的域
     * 直接返回值
     * @param obj
     * @param fieldName
     * @return
     * @throws NoSuchFieldException
     * @throws SecurityException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    public Object getField(Object obj,String fieldName) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
    	Field field = obj.getClass().getField(fieldName);
    	Object ret = field.get(obj);
    	return ret;
    }
}  